package com.hyz.service.assetManager.impl;

import com.alibaba.fastjson.JSON;
import com.hyz.client.AssetClient;
import com.hyz.client.LogClient;
import com.hyz.pojo.assetManager.AssetInboundRequest;
import com.hyz.pojo.assetManager.AssetInboundStatusResponse;
import com.hyz.service.assetManager.AssetManagerService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pojo.ResponseResult;
import pojo.asset.Asset;
import pojo.reposiroty.Repository;
import util.GetValue;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * @author HGD
 * @date 2022/9/29 23:02
 */
@Service
public class AssetManagerServiceImpl implements AssetManagerService {

    @Resource
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private AssetClient assetClient;

    @Autowired
    private LogClient logClient;


    @Override
    public ResponseResult<List<Asset>> getAllUnassignedAssets() {
        List<Asset> allUnassignedAssets = assetClient.getAllUnassignedAssets();
        return new ResponseResult<>(200, "成功搜索数据", allUnassignedAssets);
    }

    @Override
    public ResponseResult<List<Repository>> getAllRepositories() {
        List<Repository> allRepositories = assetClient.getAllRepositories();
        return new ResponseResult<>(200, "成功搜索数据", allRepositories);
    }


    /**
     * 创建资产分库
     *
     * @param assetInboundRequest 资产分库信息
     * @return 是否分库成功
     */
    @Override
    public ResponseResult<String> createAssetInbound(AssetInboundRequest assetInboundRequest) {
        if (Objects.isNull(assetInboundRequest)) {
            return new ResponseResult<>(400, "参数错误", null);
        }
        // TODO: 通过 Spring Security 获取到用户id和用户的权限
        Long assetManagerId = new GetValue().getUuid();
        // TODO: 通过 Spring Security 获取用户职责
        String right = "assetManager";
        // 设置 assetManagerId
        assetInboundRequest.setAssetManagerId(assetManagerId);
        assetInboundRequest.setId(new GetValue().getUuid());
        // 将对象序列化成 Json 对象
        String massage = JSON.toJSONString(assetInboundRequest);
        // 2. 通过消息队列将 assetInboundRequest 发送至 Log 服务当中
        rabbitTemplate.convertAndSend(right, "assetInbound", massage);
        return new ResponseResult<>(200, "分库申请申请正在审批，请耐心等待", null);
    }

    @Override
    public List<AssetInboundStatusResponse> getAssetInboundStatus() {
        // TODO: 通过 SpringSecurity 获取用户的id
        Long assetManagerId = 1L;
        return logClient.getAssetInboundStatus(assetManagerId);
    }
}
